; docformat = 'rst'
;
;+
;
; :Purpose:
;   Get simulation parameter from {sim_name}.params file in {input_directory}/{sim_name} directory.
;   Lines in {sim_name}.params MUST be written in IDL syntax.
;   
; :Inputs:
;   Sim_name: (string) Simulation name
;   Parameter: (string) Parameter name, as it appears in {sim_name}.params
; 
; :Requires:
;   {sim_name}.params
;   
; :Outputs:
;   Parameter, in data type as defined in {sim_name}.params file
;   
; :Example::
;   Get simulation start year
; 
;   IDL> StartY=CELS_GET_PARAMETER('SF6', 'STARTY')
;   
;   IDL> print, StartY
;           2007
;           
; :History:
; 	Written by: Matt Rigby, MIT, Aug 19, 2011
;
;-
function cels_get_parameter, sim_name, parameter

  compile_opt idl2
  on_error, 2
  
  n_parameters=n_elements(parameter)
  if (n_parameters gt 1) then begin
    Message, 'Only one parameter allowed'
  endif
  
  fname=cels_filestr(/Input, sim_name + '/' + sim_name + '.params')
  
  lines=file_lines(fname)
  params=strarr(lines)

  ;Read file
  openr, FUN, fname, /get_lun
    str=''
    for li=0, Lines-1 do begin
      readf, FUN, str
      params[li]=str
    endfor
  close, FUN
  free_lun, FUN

  ;Get rid of comment and white space lines
  first_char=strmid(params, 0, 1)
  wh=where(first_char ne ';' and first_char ne '' and first_char ne ' '); and strmid(params, 0, 3) ne 'LE_')
  if wh[0] ne -1 then begin
    params=strtrim(params[wh], 2)
  endif

  ;Output parameter
  pi=0
  parameter_string_length=strlen(parameter[pi])
  wh=where(StrUpCase(strmid(params, 0, parameter_string_length)) eq StrUpCase(strtrim(parameter[pi], 2)), count)
  if (count gt 0) then begin
    result=execute(params[wh[0]])
    result=execute( strcompress(  'out_param='+strmid(strtrim(params[wh[0]], 2), 0, parameter_string_length) ) )
    return, out_param
  endif else begin
    print, "CELS_GET_PARAMETER: ", parameter[wh[0]], " not present"
    return, !null
  endelse  

End